var gpsLonLatArray;
var gpsStartTime;
var gpsLineName;
///往服务器上去数据的间隔  毫秒
var gpsTimeOut = 5000;
var openGPSFlag = false;
//绘制线路的图层
var gpsLineLayer;
//绘制坐标的mark图层
var gpsMarkLayer;
//任意点轨迹和灾害点轨迹的标志位
var ryOrZhFlag ;

//点击我的位置绘一个标
var wodeweizhiMark;
//点击我的位置标的图层
var wodeweizhiMarkLayer;

//初始化gps功能菜单的按钮事件
function initGPSFunction(){
	$("#qidonggps").click(qidonggpsClickFunction);
	$("#wodeweizhi").click(wodeweizhiClickFunction);
	$("#kaishidingwei").click(kaishidingweiClickFunction);
	$("#jieshudingwei").click(jiesudingweiClickFunction);
	$("#clearweizhi").click(clearweizhiClickFunction);
}

//清除点击我的位置之后的标
function clearweizhiClickFunction(){
	if(wodeweizhiMarkLayer){
		map.removeLayer(wodeweizhiMarkLayer);
		wodeweizhiMarkLayer = undefined;
		wodeweizhiMark = undefined;
	}
}

//启动gps功能按钮的点击事件//////////////////////////////////////////////////
function qidonggpsClickFunction(){
	//去后台调用平板上的启动串口功能,发送请求
	var url = "GetGPSInfo.shtml";
	$.post(url,null,qidongCallback);
}

//启动gps的回调函数
function qidongCallback(res){
	if(res=="true"){
		openGPSFlag = true;
		alert("gps启动成功!");
	}else{
		openGPSFlag = false;
		alert("gps启动失败!");
	}
}


//我的位置功能按钮的点击事件//////////////////////////////////////////////
function wodeweizhiClickFunction(){
//	if(!openGPSFlag){
//		alert("请先开启gps");
//		return;
//	}
	wodeweizhiDlgInit();
}

//开始定位功能按钮的点击事件////////////////////////////////////////////////
function kaishidingweiClickFunction(){
	changeAllButtonUseable("start");
	kaishidingweiDlgInit();
}

//按钮初始化状态
function changeAllButtonUseable(flag){
	if(flag=="start"){
		$("#qidonggps").prop("disabled",true);
		$("#wodeweizhi").prop("disabled",true);
		$("#kaishidingwei").prop("disabled",true);
		$("#jieshudingwei").prop("disabled",false);
		$("#lishiguiji").prop("disabled",true);
	}else{
		$("#qidonggps").prop("disabled",false);
		$("#wodeweizhi").prop("disabled",false);
		$("#kaishidingwei").prop("disabled",false);
		$("#jieshudingwei").prop("disabled",true);
		$("#lishiguiji").prop("disabled",false);
	}
}

function moverCar(){
	var url = "Getjwd.shtml";
	$.post(url,null,moverCarCallback);
}

function moverCarCallback(res){
	//需要添加判断
	if(res=="0.0,0.0,0.0")
		return;
	gpsLonLatArray.push(res);
	t2 = setTimeout("moverCar()", gpsTimeOut);
	//实时绘制的坐标，线路等功能
	drawLineAndMarkForGPS();
}

//结束定位功能按钮的点击事件/////////////////////////////////////////////////////
function jiesudingweiClickFunction(){
	clearTimeout(t2);
	var lonlatStr = gpsLonLatArray.join(";");
	var url = "";
	if(ryOrZhFlag){
		url = "insertGpsGuiji.shtml";
	}else{
		url = "insertGpsGuijiForZhdn.shtml";
	}
	var obj ={time:gpsStartTime,lonlatStr:lonlatStr,name:gpsLineName};
	$.post(url,obj,jiesudingweiCallback);
}

function jiesudingweiCallback(res){
	if(res=="true"){
		alert("插入成功!");
		gpsLonLatArray = [];
		gpsStartTime = "";
		gpsLineName = "";
		//各种清除
		removeAllLayer();
		changeAllButtonUseable('over');
	}else{
		alert("插入失败!");
	}
}

//历史轨迹功能按钮的点击事件//////////////////////////////////////////////////////
function lishiguijiClickFunction(){
	//出现对话框，可以进行条件查询,如果要分页，后期再加
	//调用forgpslinequery.js的函数
	initLishiguijiQueryDlg();
}

/////////////////gps关于图层的处理///////////////////////////////////////////////
function removeAllLayer(){
	if(gpsLineLayer){
		map.removeLayer(gpsLineLayer);
		gpsLineLayer = undefined;
	}
	if(gpsMarkLayer){
		map.removeLayer(gpsMarkLayer);
		gpsMarkLayer = undefined;
	}
}

function drawLineAndMarkForGPS(){
	gpsLineLayer.removeAllFeatures();
	gpsMarkLayer.clearMarkers();
	var xyStr = gpsLonLatArray[gpsLonLatArray.length-1];
	var line = createGPSLine(gpsLonLatArray);
	var mark = createGPSMark(xyStr);
	//如果mark打在了当前显示范围之外，那么居中
    checkMarkLonLatInCurrentView(mark);
	gpsLineLayer.addFeatures(line);
	gpsMarkLayer.addMarker(mark);
}


//看mark是否是在地图可见范围内
function checkMarkLonLatInCurrentView(mark){
	var bound = map.getExtent();
	var left = bound.left;
	var right = bound.right;
	var bottom = bound.bottom;
	var top = bound.top;
	var lon = mark.lonlat.lon;
	var lat = mark.lonlat.lat;
	if(lon>Number(left)&&lat<Number(right)&&lat>Number(bottom)&&lat<Number(top)){
		return;
	}
	map.setCenter(mark.lonlat);
}

function createGPSLine(gpsLonLatArr){
	var lonlatPointsArray = [];
	for(var i =0;i<gpsLonLatArr.length;i++){
		var x = gpsLonLatArr[i].split(",")[0];
		var y = gpsLonLatArr[i].split(",")[1];
		var point = new OpenLayers.Geometry.Point(x,y);
		lonlatPointsArray.push(point);
	}
	var line = new OpenLayers.Geometry.LineString(lonlatPointsArray);
	var style = new OpenLayers.Util.extend({ fill: true, stroke: true }, {
		fillColor: "#F06F69",
        fillOpacity: 0.9, 
        hoverFillColor: "white",
        hoverFillOpacity: 0.9,
        strokeColor: "#ee9900",
        strokeOpacity: 1,
        strokeWidth: 4,
        strokeLinecap: "round",
        strokeDashstyle: "solid",
        hoverStrokeColor: "red",
        hoverStrokeOpacity: 1,
        hoverStrokeWidth: 0.2,
        pointRadius: 6,
        hoverPointRadius: 1,
        hoverPointUnit: "%",
        pointerEvents: "visiblePainted",
        cursor: "inherit"
	});
	var feature =new OpenLayers.Feature.Vector(line,null,style);
	return feature;
}

function createGPSMark(xyStr){
	var x = Number(xyStr.split(",")[0]);
	var y = Number(xyStr.split(",")[1]);
	var size = new OpenLayers.Size(16, 16);
	var icon = new OpenLayers.Icon('image/car.png', size);
	var mark = new OpenLayers.Marker(new OpenLayers.LonLat(x,y),icon);
	return mark;
}
